package app.qrcode.browser.todoapp.fragments.list.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.todoapp.R
import app.qrcode.browser.todoapp.data.models.Priority
import app.qrcode.browser.todoapp.data.models.TaskStatus
import app.qrcode.browser.todoapp.data.models.ToDoData
import app.qrcode.browser.todoapp.data.viewmodel.ToDoViewModel
import app.qrcode.browser.todoapp.fragments.SharedViewModel
import app.qrcode.browser.todoapp.fragments.list.OnItemClickListener

/**
 * Created by YiGo on 2022/5/8.
 */
class ListAdapter(val fragment: Fragment) : RecyclerView.Adapter<ListAdapter.ListViewHolder>() {

    var dataList = emptyList<ToDoData>()


    var listener: OnItemClickListener? = null


    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.listener = listener
    }

    private val mSharedViewModel by lazy {
        ViewModelProvider(fragment.requireActivity()).get(
            SharedViewModel::class.java
        )
    }

    private val mToDoViewModel by lazy {
        ViewModelProvider(fragment.requireActivity()).get(
            ToDoViewModel::class.java
        )
    }


    override fun getItemViewType(position: Int): Int {
        return position
    }


    inner class ListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val titleTV = itemView.findViewById<TextView>(R.id.title_txt)
        val descripionTV = itemView.findViewById<TextView>(R.id.description_txt)
        val rowCLayout =
            itemView.findViewById<androidx.constraintlayout.widget.ConstraintLayout>(R.id.rowCLayout)
        val priorityIndicatorCV =
            itemView.findViewById<androidx.cardview.widget.CardView>(R.id.priority_indicator)
        var listTaskStatusTV = itemView.findViewById<TextView>(R.id.list_taskStatus_tv)
        var noticeTimeTV = itemView.findViewById<TextView>(R.id.time_tv)
        var notificationIV = itemView.findViewById<ImageView>(R.id.notification_iv)
    }

    lateinit var viewHolder: ListViewHolder

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
//        return when (viewType) {
//            getItemViewType()
//        }
        val view = LayoutInflater.from(parent.context).inflate(R.layout.row_layout, parent, false)
        viewHolder = ListViewHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
        var data = dataList[position]
        holder.titleTV.text = data.title
        holder.descripionTV.text = data.descriptor
        holder.listTaskStatusTV.text = when (data.taskStatus) {
            TaskStatus.TODO -> {
                holder.listTaskStatusTV.setTextColor(
                    ContextCompat.getColor(
                        holder.itemView.context,
                        R.color.red
                    )
                )
                mSharedViewModel.parseTaskStatusToString(data.taskStatus)
            }
            TaskStatus.QUALIFY_INSTANCE -> {
                holder.listTaskStatusTV.setTextColor(
                    ContextCompat.getColor(
                        holder.itemView.context,
                        R.color.yellow
                    )
                )
                mSharedViewModel.parseTaskStatusToString(data.taskStatus)
            }
            TaskStatus.DONE -> {
                holder.listTaskStatusTV.setTextColor(
                    ContextCompat.getColor(
                        holder.itemView.context,
                        R.color.green
                    )
                )
                mSharedViewModel.parseTaskStatusToString(data.taskStatus)
            }
        }
//        holder.listTaskStatusTV.setOnClickListener {
////            showChangeTaskStatusPopUpMenu(it, data,position)
//            listener?.onItemClick(viewHolder, it)
//        }
        holder.noticeTimeTV.text =
            if (data.date == null) "" else data.date +" " +if (data.time == null) "" else data.time
        holder.notificationIV.visibility = if (data.isNoticed) View.VISIBLE else View.GONE
        mSharedViewModel.parseTaskStatusToString(data.taskStatus)
        holder.rowCLayout.setOnClickListener {
            val bundle = bundleOf("item" to data)
            holder.itemView.findNavController()
                .navigate(R.id.action_listFragment_to_updateFragment, bundle)
        }
        val priority = data.priority
        when (priority) {
            Priority.HIGH -> holder.priorityIndicatorCV.setCardBackgroundColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.red
                )
            )
            Priority.MEDIUM -> holder.priorityIndicatorCV.setCardBackgroundColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.yellow
                )
            )
            Priority.LOW -> holder.priorityIndicatorCV.setCardBackgroundColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.green
                )
            )
        }


    }

//    override fun getItemId(position: Int): Long {
//        return position.toLong()
//    }

    override fun getItemCount(): Int = dataList.size

    fun setData(toDoData: List<ToDoData>) {
        val toDoDiffUtil = ToDoDiffUtil(dataList, toDoData)
        val toDoDiffResult = DiffUtil.calculateDiff(toDoDiffUtil)
        this.dataList = toDoData
        toDoDiffResult.dispatchUpdatesTo(this)
    }

}